package risk.client.logic;

import java.awt.Color;

import java.awt.color.ColorSpace;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

 /**
  * @author: Pierre
  */
 public class Node implements java.io.Serializable {
    private final int DEFAULTDIAMETER = 70;
    private final Color DEFAULTCOLOR = Color.GRAY;
    
    private boolean isDrawn = false;
    private int id, x, y, diameter;
    private Army army;
    private Color color;
    private List<Node> connections;
     
    /**
     * Create a new node
     * 
     * @author: Pierre
     * @param id
     * @param x
     * @param y
     */
    public Node(int id, int x, int y) {
        this.x = x;
        this.y = y;
        this.id = id;
        
        color = DEFAULTCOLOR;
        diameter = DEFAULTDIAMETER;
        connections = new ArrayList<Node>();
        army = new Army(1, null);
    }
     
    /**
     * Add a new connection
     * 
     * @author: Pierre
     * @param hook
     */
    public void addConnection(Node hook) {
        try {
            if(!isConnected(hook)) {
                connections.add(hook);
                hook.addConnection(this, true);
            }
        } catch(NullPointerException e) {
            System.out.println(e.toString());
        }
    }
    
    /**
     * Add a connection
     * 
     * @author: Pierre
     * @param hook
     * @param oneway
     */
    public void addConnection(Node hook, boolean oneway) {
        //Overloading addConnection with ondeWay to prevent A to B to A to B to...
        try {
            connections.add(hook);
        } catch(NullPointerException e) {
            System.out.println(e.toString());
        }
    }
    
    /**
     * Delete connection
     * 
     * @author: Pierre
     * @param hook
     * @param oneway
     */
    public void delConnection(Node hook, boolean oneway) {
        connections.remove(hook);
    }
    
    /**
     * Delete connection
     * 
     * @author: Pierre
     * @param hook
     */
    public void delConnection(Node hook) {
        connections.remove(hook);
        hook.delConnection(this, true);
    }
    
    /**
     * Return true if equal to object
     * 
     * @author: Pierre
     * @param o
     * @return bool
     */
    public boolean equals(Object o) {
        if(o == null || !(o instanceof Node)){
            return false;
        }

        Node n = (Node)o;
        return (n.getId() == getId());
    }
    
    /**
     * return id
     * 
     * @author: Pierre
     * @return id
     */
    public int hashCode() {
        return id;
    }
    
    /**
     * Return true if connected
     * 
     * @author: Pierre
     * @param node
     * @return bool
     */
    public boolean isConnected(Node node) {
        Iterator i = connections.iterator();
      
        while(i.hasNext()) {
            if(i.next() == node) return true;
        }
      
        return false;
    }
    
    // One liners..
    public Army getArmy() { return army; };
    public List<Node> getConnections() { return connections; }    
    public Color getColor() { return color; };
    public int getDiameter() { return diameter; };
    public int getId() { return id; };
    public int getX() { return x; };
    public int getY() { return y; };
    public boolean isDrawn() { return isDrawn; };
    public void setColor(Color color) { this.color = color; };    
    public void setDiameter(int d) { diameter = d; };
    public void setDrawn(boolean b) { isDrawn = b; };
    public void setX(int x) { this.x = x; };
    public void setY(int y) { this.y = y; };    
    public Player getOwner() { return army.getOwner(); }
    public String toString() { return ("Node(" + id + ")"); };
}
